package cn.edu.scau.cmi.zengGuanSheng.springAndhibernate.client;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;

import java.util.List;

import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.zengGuanSheng.springAndhibernate.dao.StudentDao;
import cn.edu.scau.cmi.zengGuanSheng.springAndhibernate.dao.TeacherDao;
import cn.edu.scau.cmi.zengGuanSheng.springAndhibernate.domain.Student;
import cn.edu.scau.cmi.zengGuanSheng.springAndhibernate.domain.Teacher;
import cn.edu.scau.cmi.zengGuanSheng.springAndhibernate.util.SpringApplicationContextUtil;
import javafx.event.ActionEvent;

import javafx.scene.control.TextArea;

public class SpringAndHibernateController {
	@FXML
	private TextField tf_tutorId;
	@FXML
	private TextField tf_tutorName;
	@FXML
	private TextField tf_StuId;
	@FXML
	private TextField tf_teacherName;
	@FXML
	private TextField tf_StuName;
	@FXML
	private Button btn_addTutor;
	@FXML
	private Button btn_listTutor;
	@FXML
	private Button btn_addStu;
	@FXML
	private Button btn_listStu;
	@FXML
	private Label lb;
	@FXML
	private TextArea textArea_show;
	
	private static ApplicationContext applicationContext = SpringApplicationContextUtil.getApplicationContext();
	private TeacherDao teaDao = (TeacherDao)applicationContext.getBean("teacherDAO");
	private StudentDao stuDao = (StudentDao)applicationContext.getBean("studentDAO");

	/**
	 * 添加教师
	 * @param event
	 */
	// Event Listener on Button[#btn_addTutor].onAction
	@FXML
	public void eventAddTutor(ActionEvent event) {
		// TODO Autogenerated
		long tutorId = 0;
		String tutorName = null;
		try {
			tutorId = Long.parseLong(tf_tutorId.getText());
			tutorName = tf_tutorName.getText();
			Teacher t = new Teacher();
			t.setId(tutorId);
			t.setName(tutorName);
			teaDao.addTeacher(t);
			
			tf_tutorId.setText(null);
			tf_tutorName.setText(null);
			dialogInfo("工号：" + tutorId + "姓名：" + tutorName + "的教师添加成功！");
		}catch(NumberFormatException e){
			//e.printStackTrace();
			dialogWARNING("输入格式有误");
		}
	}
	
	/**
	 * 列出所有教师
	 * @param event
	 */
	// Event Listener on Button[#btn_listTutor].onAction
	@FXML
	public void eventListTutor(ActionEvent event) {
		// TODO Autogenerated
		lb.setText("教师信息列表");
		textArea_show.setText(null);
		List<Teacher> listOfTeacher = teaDao.listTeacher();
		for(Teacher t : listOfTeacher) {
			textArea_show.appendText("工号："+t.getId()+" 姓名："+t.getName()+"\r\n");
		}
	}
	
	/**
	 * 添加学生并指定指导教师
	 * @param event
	 */
	// Event Listener on Button[#btn_addStu].onAction
	@FXML
	public void eventAddStu(ActionEvent event) {
		// TODO Autogenerated
		long stuId = 0;
		String stuName = null;
		String teacherName = null;
		try {
			stuId = Long.parseLong(tf_StuId.getText());
			stuName = tf_StuName.getText();
			teacherName = tf_teacherName.getText();
			Student stu = new Student();
			stu.setId(stuId);
			stu.setName(stuName);
			Teacher t = new Teacher();
			t.setName(teacherName);
			Teacher t1 = teaDao.findTeacherByName(t);
			stu.setTeacher(t1);
			stuDao.addStu(stu);
			
			tf_StuId.setText(null);
			tf_StuName.setText(null);
			tf_teacherName.setText(null);
			dialogInfo("学号：" + stuId + "姓名：" + stuName + "导师："+teacherName+"的学生添加成功！");
			
		}catch(NumberFormatException e){
			//e.printStackTrace();
			dialogWARNING("输入格式有误");
		}catch(IndexOutOfBoundsException e) {
			//e.printStackTrace();
			dialogERROR("对不起没有这个教师！请重新输入");
		}
	}
	
	/**
	 * 列出所有学生
	 * @param event
	 */
	// Event Listener on Button[#btn_listStu].onAction
	@FXML
	public void eventListStu(ActionEvent event) {
		// TODO Autogenerated
		lb.setText("学生信息列表");
		textArea_show.setText(null);
		List<Student> listOfStudent = stuDao.listStu();
		for(Student stu : listOfStudent) {
			textArea_show.appendText("学号："+stu.getId()+" 姓名："+stu.getName()+" 导师："+stu.getTeacher().getName()+"\r\n");
		}
	}
	
	public void dialogInfo(String message) {
		Alert alert = new Alert(AlertType.INFORMATION);
		alert.setTitle("提示");
		alert.setHeaderText(null);
		alert.setContentText(message);
		alert.showAndWait();
	}
	
	public void dialogWARNING(String message) {
		Alert alert = new Alert(AlertType.WARNING);
		alert.setTitle("警告");
		alert.setHeaderText(null);
		alert.setContentText(message);
		alert.showAndWait();
	}
	
	public void dialogERROR(String message) {
		Alert alert = new Alert(AlertType.ERROR);
		alert.setTitle("错误");
		alert.setHeaderText(null);
		alert.setContentText(message);
		alert.showAndWait();
	}
}
